package com.dragonwu.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.dragonwu.service.EasyCaptchaService;
import com.dragonwu.utils.RedisUtil;
import com.dragonwu.utils.SnowflakeIdGen;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.ChineseCaptcha;
import com.wf.captcha.ChineseGifCaptcha;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class EasyCaptchaServiceImpl implements EasyCaptchaService {
    /*
    redis工具
     */
    @Autowired
    private RedisUtil redisUtil;

    /*
    雪花算法生成唯一key
     */
    @Autowired
    private SnowflakeIdGen snowflakeIdGen;

    @Override
    public Map<String, String> outputCaptchaImg(Integer type) throws IOException {
        String codeValue = "";
        Map<String, String> map = new HashMap<>();
        switch (type) {
            case CAPTCHA_TYPE_DEFAULT_PNG:
                SpecCaptcha specCaptcha = new SpecCaptcha(CAPTCHA_WIDTH, CAPTCHA_HEIGHT, CAPTCHA_LENGTH);
                codeValue = specCaptcha.text().toLowerCase();
                map.put(EasyCaptchaService.IMAGE, specCaptcha.toBase64());
                break;
            case CAPTCHA_TYPE_GIF:
                GifCaptcha gifCaptcha = new GifCaptcha(CAPTCHA_WIDTH, CAPTCHA_HEIGHT, CAPTCHA_LENGTH);
                codeValue = gifCaptcha.text().toLowerCase();
                map.put(EasyCaptchaService.IMAGE, gifCaptcha.toBase64());
                break;
            case CAPTCHA_TYPE_CHINESE:
                ChineseCaptcha chineseCaptcha = new ChineseCaptcha(CAPTCHA_WIDTH, CAPTCHA_HEIGHT, CAPTCHA_LENGTH);
                codeValue = chineseCaptcha.text();
                map.put(EasyCaptchaService.IMAGE, chineseCaptcha.toBase64());
                break;
            case CAPTCHA_TYPE_GIF_CHINESE:
                ChineseGifCaptcha chineseGifCaptcha = new ChineseGifCaptcha(CAPTCHA_WIDTH, CAPTCHA_HEIGHT, CAPTCHA_LENGTH);
                codeValue = chineseGifCaptcha.text();
                map.put(EasyCaptchaService.IMAGE, chineseGifCaptcha.toBase64());
                break;
            case CAPTCHA_TYPE_ARITHMETIC:
                ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(CAPTCHA_WIDTH, CAPTCHA_HEIGHT);
                codeValue = arithmeticCaptcha.text();
                map.put(EasyCaptchaService.IMAGE, arithmeticCaptcha.toBase64());
                break;
            default:
                throw new IOException("类型不存在");
        }
        //生成redis键名
        String uuid = String.valueOf(snowflakeIdGen.nextId());
        //存入redis
        redisUtil.setObject(EasyCaptchaService.CAPTCHA_KEY_PRE + uuid, codeValue, EasyCaptchaService.CAPTCHA_CACHE_TIME, TimeUnit.MINUTES);
        map.put(EasyCaptchaService.UUID, uuid);
        return map;
    }

    @Override
    public void verifyCaptchaCode(String uuid, String captchaCode) throws RuntimeException {
        String cacheCaptchaCode = (String) redisUtil.getObject(EasyCaptchaService.CAPTCHA_KEY_PRE + uuid);
        if (StringUtils.isBlank(cacheCaptchaCode)) {
            throw new RuntimeException("验证码已过期，请重新获取");
        }
        if (!cacheCaptchaCode.equals(captchaCode.toLowerCase())) {
            throw new RuntimeException("验证码错误");
        }
    }
}
